﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.SystemManage
{
    public partial class HospitalInfo : BasePage
    {
        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_Area ia = Factory.BLL_Area();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetControl();
                GetInfo();
            }
        }

        /// <summary>
        /// 获取医疗机构信息
        /// </summary>
        private void GetInfo()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["hID"]))
            {
                int temp;
                string hID = Request.QueryString["hID"];
                //Hospital hospital = ih.GetHospitalByWhere(new Model.Query.Hospital { HospitalID = hID }, 1, 0, out temp)[0];

                Model.HospitalInfo hospital = ih.GetHospitalInfoByID(hID);
                lblHospitalID.Text = hospital.HospitalID;
                lblHospitalName.Text = hospital.HospitalName;
                lblHospitalAdress.Text = hospital.Address;
                lblContactTel.Text = hospital.TelNo;
                lblContactPerson.Text = hospital.ContactPerson;
                lblMobile.Text = hospital.CellNo;
                lblEmail.Text = hospital.Email;

                txtContactPerson.Text = hospital.ContactPerson;
                txtContactTel.Text = hospital.TelNo;
                txtHospitalAddress.Text = hospital.Address;
                txtHospitalName.Text = hospital.HospitalName;
                txtHospitalID.Text = hospital.HospitalID;

                txtMobile.Text = hospital.CellNo;
                txtEmail.Text = hospital.Email;
                dplLevel.SelectedValue = hospital.Levels;
                hfdAreaId.Value = hospital.AreaID;

                #region 内蒙的

                dplLevelDetail.SelectedValue = hospital.LevelDetail;
                txtCode.Text = hospital.Code;
                ddlNature.SelectedValue = hospital.Nature;
                ddlEconomicType.SelectedValue = hospital.EconomicType;
                ddlOrganizers.SelectedValue = hospital.Organizers;
                ddlAffiliation.SelectedValue = hospital.Affiliation;
                txtBeds.Text = hospital.Beds;
                rblistCooperative.SelectedValue = hospital.IsCooperative ? "1" : "0";
                rblistInsurance.SelectedValue = hospital.IsInsurance ? "1" : "0";
                txtExpenditure.Text = hospital.Expenditure;
                rblistOld.SelectedValue = hospital.IsOld ? "1" : "0";

                #endregion

                //Area area = ia.GetArea(hospital.AreaID);
                //lblArea.Text = area.AreaName;

                lblArea.Text = GetFullAreaName(hospital.AreaID);
            }
        }
        /// <summary>
        /// 获取地区全路径
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        private string GetFullAreaName(string areaId)
        {
            string fullAreaName = "";
            IBLL_Area areaCtrl = Factory.BLL_Area();

            Area area = areaCtrl.GetArea(areaId);
            fullAreaName = area.AreaName;
            if (area.FatherAreaID != "0")
            {
                Area father = areaCtrl.GetArea(area.FatherAreaID);
                fullAreaName = father.AreaName + ">>" + fullAreaName;


                if (father.FatherAreaID != "0")
                {
                    Area father2 = areaCtrl.GetArea(father.FatherAreaID);
                    fullAreaName = father2.AreaName + ">>" + fullAreaName;

                    //this.ddlProvice.SelectedValue = father2.AreaID;
                    //this.ddlCity.SelectedValue = father.AreaID;
                    //this.ddlCountry.SelectedValue = areaId;
                }
                else
                {
                    //this.ddlProvice.SelectedValue = father.AreaID;
                    //this.ddlCity.SelectedValue = area.AreaID;
                }
            }
            else
            {
                //this.ddlProvice.SelectedValue = areaId;
            }

            return fullAreaName;
        }
        /// <summary>
        /// 显示不同的控件
        /// </summary>
        private void GetControl()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["flag"]))
            {
                string flag = Request.QueryString["flag"];
                if (flag == "1")
                {
                    lblArea.Visible = true;
                    lblHospitalID.Visible = true;
                    lblContactPerson.Visible = true;
                    lblContactTel.Visible = true;
                    lblHospitalAdress.Visible = true;
                    lblHospitalName.Visible = true;
                    txtContactPerson.Visible = false;
                    txtContactTel.Visible = false;
                    txtHospitalAddress.Visible = false;
                    txtHospitalName.Visible = false;
                    txtHospitalID.Visible = false;
                }
                else if (flag == "2")
                {
                    lblHospitalID.Visible = true;
                    lblArea.Visible = true;
                    lblContactPerson.Visible = false;
                    lblContactTel.Visible = false;
                    lblHospitalAdress.Visible = false;
                    lblHospitalName.Visible = false;
                    txtContactPerson.Visible = true;
                    txtContactTel.Visible = true;
                    txtHospitalAddress.Visible = true;
                    txtHospitalName.Visible = true;
                    txtHospitalID.Visible = false;
                }
                else if (flag == "3")
                {
                    lblHospitalID.Visible = false;
                    lblArea.Visible = false;
                    lblContactPerson.Visible = false;
                    lblContactTel.Visible = false;
                    lblHospitalAdress.Visible = false;
                    lblHospitalName.Visible = false;
                    txtContactPerson.Visible = true;
                    txtContactTel.Visible = true;
                    txtHospitalAddress.Visible = true;
                    txtHospitalName.Visible = true;
                    txtHospitalID.Visible = true;
                }
            }
        }

        /// <summary>
        /// 返回不同的提示信息
        /// </summary>
        /// <returns></returns>
        public string GetString()
        {
            string Info = "";
            if (!string.IsNullOrEmpty(Request.QueryString["flag"]))
            {
                string flag = Request.QueryString["flag"];
                //查看详情
                if (flag == "1")
                {
                    Info = "查看医疗机构信息";
                }
                //修改详情
                else if (flag == "2")
                {
                    Info = "修改医疗机构信息";
                }
                //新增
                else if (flag == "3")
                {
                    Info = "新增医疗机构";
                }
            }
            else
            {
                Info = "查看医疗机构信息";
            }
            return Info;
        }

        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }
        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
    }
}
